import {fetchList,addObj,delObj} from '../api/annualaudittemplate'
export default  {
    data(){
        return{
            tableData:[
                {
                    conditionTm:'',
                    fixedStartTm:'',
                    annualStatus:'',
                    auditContent:''
                }
            ],
            radio:'1',
            radio1:'1',
            value:'',
            conditionTm: '',
            fixedStartTm:[],
            disabled:false,
            options: [{
                value: 5,
                name: '提前5天设置'
            }, {
                value: 10,
                name: '提前10天设置'
            }, {
                value: 15,
                name: '提前15天设置'
            }],
        }
    },
    methods: {
        add() {
            let data = {
                conditionTm: '',
                fixedStartTm: '',
                fixedEndTm: '',
                annualStatus: '',
                auditContent: ''
            }
            this.tableData.push(data)
        },
        del(item) {
            if(typeof(item.row.auditTemplateId)=='undefined'){
                this.tableData.splice(item.$index,1)
            }else{
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>
                    delObj(item.row.auditTemplateId).then(resp => {
                        if (resp.data.data) {
                            this.suc('删除成功')
                            this.Rest()
                            this.getDataList()
                        } else {
                            this.err('删除失败')
                        }
                    })
                )

            }

        },
        change(item) {

        },
        getDataList() {
            fetchList({}).then(res => {
                if (res) {
                    this.tableData = res.data.data.records
                    if(this.tableData[0].annualRemit=='1'){
                        this.radio=this.tableData[0].annualRemit
                        if (this.tableData[0].conditionTm) {
                            this.radio1 = '1'
                            this.conditionTm = this.tableData[0].conditionTm

                        } else if (this.tableData[0].fixedStartTm) {
                            this.radio1 = '2'
                            let start = new Date(this.tableData[0].fixedStartTm)
                            let end = new Date(this.tableData[0].fixedEndTm)
                            this.fixedStartTm.push(start)
                            this.fixedStartTm.push(end)
                        }
                    }else{
                        this.radio=this.tableData[0].annualRemit
                    }

                    this.$forceUpdate()
                }
                this.$forceUpdate()
            })
        },
        submit() {
            if (this.radio == 1 && this.radio1 == 1 && this.conditionTm == '') {
                return this.$message.error('条件日期不能为空')
            }
            if (this.radio == 1 && this.radio1 == 2 && this.fixedStartTm.length == 0) {
                return this.$message.error('固定日期不能为空')
            }
            this.tableData.map(data => {
                data.annualRemit=this.radio
                if(this.radio=='1'){
                    if (this.radio1 == '1') {
                        data.conditionTm = this.conditionTm
                        data.fixedStartTm = null
                        data.fixedEndTm = null
                    } else if (this.radio1 == '2') {
                        data.conditionTm = null
                        data.fixedStartTm = this.getDate(this.fixedStartTm[0])
                        data.fixedEndTm = this.getDate(this.fixedStartTm[1])
                    }
                }else{
                    data.conditionTm = null
                    data.fixedStartTm = null
                    data.fixedEndTm = null
                }
            })
            let i=1
            let f=true
            this.tableData.forEach(data=>{
                if (data.auditContent.length>200) {
                   this.err("第"+i+"条数据的内容的长度不能超过200")
                    f=false
                }
                if(data.auditContent.length==0){
                    this.err("第"+i+"条数据的内容不能为空")
                    f=false
                }
                i++
            })
            if(f){
                addObj(this.tableData).then(res => {
                    if (res) {
                        this.suc('保存成功')
                        this.Rest()
                        this.getDataList()
                    }
                })
            }

        },
        getDate(data) {
            var date = new Date(data)
            return date.getFullYear() + '-' + ((date.getMonth() + 1) >= 10 ? (date.getMonth() + 1) : '0' + (date.getMonth() + 1)) + '-' + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate())
        },
        Rest() {
            this.conditionTm = ''
            this.fixedStartTm = []
        }
    }
    ,

    beforeMount(){
        this.getDataList()
    }
}
